package com.itheima.reggie.controller;

import com.itheima.reggie.commons.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Page;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags ="菜品")
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController extends BaseController{
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("page")
    public R<Page<DishDto>> findPage(@RequestParam("page") Integer pageNum, Integer pageSize, String name) {
        log.info("查询条件是：第{}页，每页{}条，菜品名字：{}", pageNum, pageSize, name);
        Page<DishDto> page = dishService.findByPage(pageNum, pageSize, name);
        return R.success(page);
    }
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        //注：看不到Dish中的属性，要设置断点才可见
        log.info("添加菜品和口味信息：{}", dishDto);
        dishService.saveWithFlavor(dishDto);
        return R.success("添加菜品成功");
    }
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") Integer status,@RequestParam Long[] ids) {
        Integer integer = dishService.updateStatus(status, ids);
        log.info("修改了"+integer+"条数据");
        return R.success("修改成功66666");
    }

    /**
     * 作用：根据id查找菜品和菜品的口味
     * @param id  菜品的id
     */
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable("id") Long id) {
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        //更新菜品信息
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    @DeleteMapping()
    public R<Integer> deleteById(@RequestParam Long[] ids) {
        int count = dishService.deleteByIds(ids);
        return R.success(count);
    }
    @GetMapping("list")
    public R<List<DishDto>> findDishById(Long categoryId,String name) {
        List<DishDto> dishList = dishService.findByCategoryId(categoryId,name,1);
        return R.success(dishList);
    }

    @GetMapping("listFlavor")
    public R<List<DishDto>> findDishByIdAndStatus(Long categoryId,String name, Integer status) {
        List<DishDto> dishList = dishService.findByCategoryId(categoryId,name, status);
        return R.success(dishList);
    }
}
